package com.ruoyi.user.service.impl;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.user.domain.DriverUser;
import com.ruoyi.user.domain.DriverUserWorkStatus;
import com.ruoyi.user.domain.OrderInfo;
import com.ruoyi.user.domain.PassengerUser;
import com.ruoyi.user.domain.vo.PassengerVo;
import com.ruoyi.user.mapper.TaxiMapper;
import com.ruoyi.user.service.TaskService;
import com.ruoyi.user.utils.VehicleChargeUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author wangqingbin
 * 不要等待机会，而要创造机会
 * @date 2024/10/21 15:29
 **/
@Service
@Slf4j
public class TaskServiceImpl implements TaskService {

    @Autowired
    TaxiMapper taxiMapper;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public AjaxResult saveOrder(PassengerVo passengerVo) {


        //根据id获取用户的信息
        PassengerUser passengerUser = taxiMapper.selectPassenger(passengerVo.getOpenid());
        //查询用户上一个订单是否支付订单
        OrderInfo orderStatus = taxiMapper.selectOrder(passengerUser.getOpenid());
        if (Optional.ofNullable(orderStatus).isPresent()) {
            return AjaxResult.error("您有未支付订单");
        }

        //创建一个空的Long类型的列表
        List<Long> DriverList = new ArrayList<>();

        //进行司机家庭地址和目的地的计算
        List<DriverUserWorkStatus> driverWorkList = taxiMapper.selectDriverWork();

        //遍历司机列表
        for (DriverUserWorkStatus driverWork : driverWorkList) {

            //计算用户与司机的距离
            Double distance = VehicleChargeUtil.calculateDistance(passengerVo.getDestLongitude(), passengerVo.getDestLatitude(), driverWork.getCurrentLatitude(), driverWork.getCurrentLongitude());


            //需要判断司机1小时内有没有预定单
            long presetTime = (long) (60 * 60 * 1000); //设置预定的时长为1小时
            Date endDateTime = new Date(VehicleChargeUtil.currentDateTime.getTime() + presetTime); //计算预定的结束时间
            OrderInfo orderInfo = taxiMapper.selectOrderInfo(driverWork.getDriverId(), VehicleChargeUtil.currentDateTime, endDateTime);

            // 如果查询到有订单信息，说明司机在接下来一小时内有预定单，跳过本次循环，继续判断下一个司机
            if (Optional.ofNullable(orderInfo).isPresent()) {
                continue;
            }
            //判断司机是否在服务中，是否在回家的路上，距离是否小于5km
            if (distance < 5 & driverWork.getWorkStatus() == 3 & driverWork.getIsHeadingHome() == 1) {
                DriverList.add(driverWork.getDriverId());
            }
            //没有顺路的乘客就查询距离3公里以内的司机
            Double distanceOrder = VehicleChargeUtil.calculateDistance(passengerVo.getDestLongitude(), passengerVo.getDestLatitude(), driverWork.getAddrLatitude(), driverWork.getAddrLongitude());
            if (driverWork.getWorkStatus() == 3 && distanceOrder <= 3) {
                DriverList.add(driverWork.getDriverId());
            }
        }
        if (DriverList.size() > 0) {
            //将integersList列表中的第一个元素设置为passengerVo对象中的driverId
            passengerVo.setDriverId(DriverList.get(0));
            //使用taxiMapper中的SelectDirverPhone方法选择integersList中的第一个元素对应的driverUser对象
            DriverUser driverUser = taxiMapper.SelectDirverPhone(DriverList.get(0));

            //将driverUser对象的driverPhone设置为passengerVo对象中的driverPhone
            passengerVo.setDriverPhone(Long.valueOf(driverUser.getDriverPhone()));
            //修改订单状态为已经接单
            passengerVo.setOrderStatus(2);
        }

        //将用户手机号放入订单中
        passengerVo.setPassengerPhone(passengerUser.getPassengerPhone());

        passengerVo.setOrderId(passengerVo.getOpenid());
        //    乘客上车时间
        passengerVo.setPickUpPassengerTime(new Date());
            //        乘客上车经纬度
        passengerVo.setPickUpPassengerLongitude(passengerVo.getDepLongitude());
            //        乘客上车经纬度
        passengerVo.setPickUpPassengerLatitude(passengerVo.getDepLatitude());


        //订单状态为未接单状态
        if (passengerVo.getOrderStatus() == null){
            passengerVo.setOrderStatus(1);
        }
        //生成订单
        taxiMapper.saveOrder(passengerVo);
        log.info("下单生成成功,等待司机接单");

        return AjaxResult.success("添加成功");
    }
}
